package com.jomoo.css.common.core.hibernate.identifier;

import com.jomoo.css.common.core.util.AssertHelper;
import com.jomoo.css.common.core.model.BaseEntity;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.IdentifierGenerator;

import java.io.Serializable;
import java.util.UUID;

//import org.hibernate.engine.spi.SharedSessionContractImplementor;

public class Ling2UUIDGenerator implements IdentifierGenerator {

    public static final String STRATEGY_NAME = "com.jomoo.css.common.core.hibernate.identifier.Ling2UUIDGenerator";

//    @Override
//    public Serializable generate(SessionImplementor sessionImplementor, Object o) throws HibernateException {
//        return UUID.randomUUID().toString();
//    }

    @Override
    public Serializable generate(SharedSessionContractImplementor sharedSessionContractImplementor, Object o) throws HibernateException {
        //持久化对象
//        ClassMetadata classMetadata=sharedSessionContractImplementor.getEntityPersister(o.getClass().getName(),o).getClassMetadata();
//        String tableName = classMetadata();//表名
//        String IdentifierPropertyName = classMetadata.getIdentifierPropertyName();//id字段名
        if (o instanceof BaseEntity) {
            BaseEntity entity = (BaseEntity) o;
            if (AssertHelper.notEmpty(entity.getId())) {
                return entity.getId();
            }
        }
        return UUID.randomUUID().toString();
    }
}